/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.micda.moroccoby.front;

import com.micda.moroccoby.common.dao.ProduitDAO;
import com.micda.moroccoby.common.entities.Produit;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sls
 */
public class PanierManager extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        //   try {
        Panier p;
        HttpSession session = request.getSession();
        session.setAttribute("test", 1);
        if (session.getAttribute("panier") == null) {
            p = new Panier();
            session.setAttribute("panier", p);
            Produit pr = new Produit();
            pr.setReference("1");
            pr.setPrix(30);
            p.getProduits().add(new LignePanier(pr, 20));
            p.getProduits().add(new LignePanier(pr, 50));
        } else {
            p = (Panier) session.getAttribute("panier");
        }

        out.print(p.getTotal());
        //}
        // String login = (String) request.getSession().getAttribute("login");
        //    if (login != null) {
        String id = request.getParameter("id");
        int ids = Integer.parseInt(id);
        int qte = 1;
        Produit prod = new Produit();
        ProduitDAO pd = new ProduitDAO();
         prod = pd.find(ids);
       // LignePanier l = new LignePanier(prod, qte);
       // p.getProduits().add(l);
        out.print(ids + " added" + p.getProduits().size() + " Au total ");
        // request.getSession().setAttribute("produit", id);
        request.getSession().setAttribute("panier", p);
        // getServletContext().getRequestDispatcher("/Panier.jsp").forward(request, response);
        // } else {   //request.setAttribute("ListJeux", l);
        // getServletContext().getRequestDispatcher("/Liste.jsp").forward(request, response);
    }
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
